package com.zareal.service.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.zareal.domain.Storage;
import com.zareal.domain.Warning;
import com.zareal.dot.GetProductDto;
import com.zareal.dot.PutApplicationDto;
import com.zareal.enums.GlobalErrorCode;
import com.zareal.feign.PurchaseFeignClient;
import com.zareal.mapper.StorageMapper;
import com.zareal.mapper.WarningMapper;
import com.zareal.properties.StockProperties;
import com.zareal.result.JSONResult;
import com.zareal.service.IStorageService;
import com.zareal.util.AssertUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Wilson
 * @since 2022-09-28
 */
@Service
public class StorageServiceImpl extends ServiceImpl<StorageMapper, Storage> implements IStorageService {
    @Autowired
    private StorageMapper storageMapper;

    @Autowired
    private StockProperties stockProperties;

    @Autowired
    private WarningMapper warningMapper;
    @Autowired
    private PurchaseFeignClient purchaseFeignClient;
    @Override
    public JSONResult putStorage(GetProductDto getProductDto) {
        // 获取商品id
        Long id = getProductDto.getId();
        // 需要入库的数量
        Integer number = getProductDto.getNumber();

        // 查询当前库中的存量
        Storage storage = storageMapper.getStorage(id);
        if (storage!=null){
            // 需要入库的数量相加
            storage.setNumber(number + storage.getNumber());
            storageMapper.putStorage(storage);
        }else {
            Storage storage1 = new Storage();
            storage1.setPruductId(id);
            storage1.setNumber(Long.valueOf(getProductDto.getNumber()));
            storageMapper.insert(storage1);
        }

        return JSONResult.success("入库成功");
    }

    @Override
    public JSONResult outStorage(GetProductDto getProductDto) {
        // 获取商品id
        Long id = getProductDto.getId();
        // 需要出库的数量
        Integer number = getProductDto.getNumber();
        // 查询当前库中的存量 库存余额不能小于10
        Storage storage = storageMapper.getStorage(id);
        Long stock = storage.getNumber();
//        if (number > stock){
//            throw  new GlobalCustomException(GlobalErrorCode.STORE_NOT_ENOUGH);
//        }
        AssertUtil.isFalse(number > stock,GlobalErrorCode.STORE_NOT_ENOUGH);
        // 需要出库的数量相加
        storage.setNumber(stock - number);
        storageMapper.putStorage(storage);
        // 出库数量太低，获取最低数量
        if (stock - number < stockProperties.getMin()){
            Warning warning = new Warning();
            warning.setWarningInfo("数量快用光了");
            warning.setDate(new Date());
            warningMapper.insert(warning);
            // 返回申请表 TODO
            PutApplicationDto putApplicationDto = new PutApplicationDto();
            putApplicationDto.setProduct_id(id);
            putApplicationDto.setNumber(500L);
            putApplicationDto.setTime(new Date());
            purchaseFeignClient.putApplication(putApplicationDto);
        }
        return JSONResult.success("出库成功");
    }
    // 查询所有缺货的商品
    @Override
    public JSONResult outOfStock() {
        List<Storage> list = storageMapper.outOfStock();
        return JSONResult.success(list);
    }
}
